package com.pbc.gmp.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.pbc.gmp.commons.utils.TreeUtils;
import com.pbc.gmp.model.OrgInfoVo;
import com.pbc.gmp.model.OrgTreeDto;
import com.pbc.gmp.model.basemodel.Org;
import com.pbc.gmp.dao.OrgMapper;
import com.pbc.gmp.service.IOrgService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xiaoleilu.hutool.convert.Convert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *   服务实现类
 * </p>
 *
 * @author kjk
 * @since 2017-04-17
 */
@Service
class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements IOrgService {

    @Resource(name="orgDao")
    private OrgMapper orgMapper;
    private List<Org> list=new ArrayList<Org>();
    @Override
    public List<Org> selectTreeGridByRootId(Long id) {
        Org org=new Org();
        EntityWrapper ew=new EntityWrapper();
        ew.where("id={0}",id);
        org=orgMapper.selectById(id);
        List<Org> childlist=selectTreeGridByPid(org.getId());
        if(childlist==null)
        {
            return null;
        }else {
            list.add(org);
            for (Org o:childlist) {
                selectTreeGridByRootId(o.getId());

            }
            return list;
        }

    }

    @Override
    public List<Org> selectTreeGridByPid(Long id) {
        EntityWrapper ew=new EntityWrapper();
        ew.where("pId={0}",id);
        return orgMapper.selectList(ew);

    }

    @Override
    public OrgTreeDto selectOrgTreeDtoById(Long id) {
        return orgMapper.selectOrgTreeDtoById(id);
    }

    @Override
    public List<OrgTreeDto> selectAllOrg() {
        return orgMapper.selectAllOrg();


    }

    @Override
    public List<Org> selectOrgListByUserId(Long id) {
        return orgMapper.selectOrgListByUserId(id);
    }

    @Override
    public List<OrgInfoVo> selectDepartmentListByUserId(Long id) {
        return orgMapper.selectDepartmentByOrgId(id);
    }

    @Override
    public List<OrgTreeDto> selectOrgByType(int type) {
        return  orgMapper.selectOrgByType(type);
    }

//    @Override
//    public List<OrgInfoVo> getDepartmentListByUserId(Long id) {
//        //得到用户组织信息
//        OrgInfoVo orgInfoVo=orgMapper.selectOrgVoListByUserId(id);
//        //根据用户组织信息得到组织下属部门列表
//        List<OrgInfoVo> departmentList=orgMapper.selectDepartmentByOrgId(orgInfoVo.getId());
//        return  departmentList;
//    }

    @Override
    public List<OrgTreeDto> selectOrgTreeDtoByPid(Long pid) {

        return orgMapper.selectOrgTreeDtoByPid(pid);
    }
    public List<OrgTreeDto>  selectOrgListByType(Long rootid ,int type) {
        List<OrgTreeDto> orgTreeDtos=selectOrgByType(type);
        OrgTreeDto orgTreeDto=TreeUtils.org2tree(orgTreeDtos, rootid);
        List<OrgTreeDto> lists=new ArrayList<OrgTreeDto>();
        lists.add(orgTreeDto);
        return lists;
    }


}
